/**
 * Copyright (C) 2008 University of Pittsburgh
 * 
 * 
 * This file is part of Open EpiCenter
 * 
 *     Open EpiCenter is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 * 
 *     Open EpiCenter is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU General Public License for more details.
 * 
 *     You should have received a copy of the GNU General Public License
 *     along with Open EpiCenter.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * 
 *   
 */
package com.hmsinc.epicenter.model.health;

import static com.hmsinc.epicenter.util.FormatUtils.formatDateTime;
import static javax.persistence.GenerationType.AUTO;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import org.apache.commons.lang.Validate;
import org.apache.commons.lang.builder.CompareToBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.ForeignKey;
import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.Parameter;
import org.hibernate.annotations.Type;
import org.hibernate.validator.Range;
import org.joda.time.DateTime;
import org.joda.time.Period;
import org.joda.time.PeriodType;

import com.hmsinc.epicenter.model.analysis.classify.Classification;
import com.hmsinc.epicenter.model.attribute.AgeGroup;
import com.hmsinc.epicenter.model.attribute.PatientClass;

/**
 * Interaction generated by hbm2java
 */
@Entity
@Table(name = "INTERACTION")
@Inheritance(strategy = InheritanceType.JOINED)
@org.hibernate.annotations.Table(appliesTo = "INTERACTION", indexes = {
		@org.hibernate.annotations.Index(name = "IDX_INTERACTION_1", columnNames = "INTERACTION_DATE"),
		@org.hibernate.annotations.Index(name = "IDX_INTERACTION_2", columnNames = "ID_PATIENT"),
		@org.hibernate.annotations.Index(name = "IDX_INTERACTION_3", columnNames = "ID_PATIENT_DETAIL"),
		@org.hibernate.annotations.Index(name = "IDX_INTERACTION_4", columnNames = "ID_PATIENT_CLASS")})
public abstract class Interaction implements HealthObject, Comparable<Interaction> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 2939717218248542896L;

	private Long id;

	private Patient patient;

	private PatientDetail patientDetail;

	private Long messageId;

	private DateTime interactionDate;

	private DateTime timestamp = new DateTime();
	
	private AgeGroup ageGroup = null;

	private Integer ageAtInteraction;

	private PatientClass patientClass;
	
	private Set<Classification> classifications = new HashSet<Classification>(0);

	// Constructors

	/** default constructor */
	public Interaction() {
	}

	/** minimal constructor */
	public Interaction(Long id) {
		this.id = id;
	}

	public Interaction(PatientClass patientClass) {
		this.patientClass = patientClass;
	}
	
	/** full constructor */
	public Interaction(Long id, Patient patient, PatientDetail patientDetail, Long messageId,
			DateTime interactionDate, Set<Classification> classifications) {
		this.id = id;
		this.patient = patient;
		this.patientDetail = patientDetail;
		this.messageId = messageId;
		this.interactionDate = interactionDate;
		this.classifications = classifications;
	}

	// Property accessors
	@GenericGenerator(name = "generator", strategy = "native", parameters = { @Parameter(name = "sequence", value = "SEQ_INTERACTION") })
	@Id
	@GeneratedValue(strategy = AUTO, generator = "generator")
	@Column(name = "ID", nullable = false, insertable = true, updatable = true)
	public Long getId() {
		return this.id;
	}

	public void setId(Long id) {
		this.id = id;
	}
	
	@ManyToOne(cascade = { CascadeType.ALL }, fetch = FetchType.LAZY)
	@JoinColumn(name = "ID_PATIENT", unique = false, nullable = false, insertable = true, updatable = true)
	@org.hibernate.annotations.ForeignKey(name = "FK_INTERACTION_2")
	public Patient getPatient() {
		return this.patient;
	}

	public void setPatient(Patient patient) {
		this.patient = patient;
	}

	@ManyToOne(cascade = { CascadeType.ALL }, fetch = FetchType.LAZY)
	@JoinColumn(name = "ID_PATIENT_DETAIL", unique = false, nullable = false, insertable = true, updatable = true)
	@org.hibernate.annotations.ForeignKey(name = "FK_INTERACTION_3")
	public PatientDetail getPatientDetail() {
		return this.patientDetail;
	}

	public void setPatientDetail(PatientDetail patientDetail) {
		this.patientDetail = patientDetail;
	}

	@Column(name = "ID_MESSAGE", unique = false, nullable = false, insertable = true, updatable = true, precision = 18, scale = 0)
	public Long getMessageId() {
		return this.messageId;
	}

	public void setMessageId(Long messageId) {
		this.messageId = messageId;
	}

	@Type(type = "joda")
	@Column(name = "INTERACTION_DATE", unique = false, nullable = false, insertable = true, updatable = true)
	public DateTime getInteractionDate() {
		return this.interactionDate;
	}

	public void setInteractionDate(DateTime interactionDate) {
		this.interactionDate = interactionDate;
	}

	/**
	 * @return the timestamp
	 */
	@Type(type = "joda")
	@Column(name = "TIMESTAMP", unique = false, nullable = false, insertable = true, updatable = true)
	public DateTime getTimestamp() {
		return timestamp;
	}

	/**
	 * @param timestamp the timestamp to set
	 */
	public void setTimestamp(DateTime timestamp) {
		this.timestamp = timestamp;
	}

	@ManyToMany(cascade = { CascadeType.PERSIST, CascadeType.MERGE }, fetch = FetchType.LAZY, targetEntity = Classification.class)
	@JoinTable(name = "INTERACTION_CLASSIFICATION", joinColumns = { @JoinColumn(name = "ID_INTERACTION") }, inverseJoinColumns = { @JoinColumn(name = "ID_CLASSIFICATION") })
	@ForeignKey(name = "FK_INTERACTION_CLASS_1")
	public Set<Classification> getClassifications() {
		return this.classifications;
	}

	public void setClassifications(Set<Classification> classifications) {
		this.classifications = classifications;
	}

	@Column(name = "AGE_AT_INTERACTION", unique = false, nullable = false, insertable = true, updatable = true, length = 3)
	@Range(min = -1, max = 200)
	public Integer getAgeAtInteraction() {
		if (this.ageAtInteraction == null) {
			if (this.getPatientDetail() == null) {
				throw new IllegalArgumentException("Patient details must be set");
			}
			if (this.getPatientDetail().getDateOfBirth() != null) {
				this.ageAtInteraction = calculateAgeFromDOB(this.getPatientDetail().getDateOfBirth());
			} else {
				this.ageAtInteraction = -1;
			}
		}
		return this.ageAtInteraction;
	}

	public void setAgeAtInteraction(Integer ageAtInteraction) {
		this.ageAtInteraction = ageAtInteraction;
	}

	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "ID_AGE_GROUP", unique = false, nullable = false, insertable = true, updatable = true)
	@org.hibernate.annotations.ForeignKey(name = "FK_INTERACTION_1")
	@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
	public AgeGroup getAgeGroup() {
		return this.ageGroup;
	}

	public void setAgeGroup(AgeGroup ageGroup) {
		this.ageGroup = ageGroup;
	}

	private int calculateAgeFromDOB(final DateTime dob) {

		int years = -1;
		if (dob != null) {
			
			Validate.isTrue(dob.isBefore(interactionDate), "Date of birth was in the future: " + dob.toString());		
			years = new Period(dob, interactionDate, PeriodType.years()).getYears();
		}
		return years;
	}

	/**
	 * @return the patientClass
	 */
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "ID_PATIENT_CLASS", unique = false, nullable = false, insertable = true, updatable = true)
	@org.hibernate.annotations.ForeignKey(name = "FK_INTERACTION_4")
	@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
	public PatientClass getPatientClass() {
		return patientClass;
	}

	/**
	 * @param patientClass the patientClass to set
	 */
	public void setPatientClass(PatientClass patientClass) {
		this.patientClass = patientClass;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Comparable#compareTo(java.lang.Object)
	 */
	public int compareTo(Interaction p) {
		return new CompareToBuilder().append(getInteractionDate(), p.getInteractionDate()).append(getId(), p.getId()).toComparison();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		return new ToStringBuilder(this).append("id", id).append("patient", patient).append(
				"patientDetail", patientDetail).append("messageId", messageId).append("interactionDate",
				formatDateTime(interactionDate)).append("ageAtInteraction", ageAtInteraction).append("ageGroup", ageGroup)
				.append("patientClass", patientClass).toString();
	}

}
